package com.fastloan.jn.mgt.process.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.application.basic.model.JnApplication;
import com.fastloan.jn.mgt.application.basic.service.JnApplicationService;
import com.fastloan.jn.mgt.customer.model.JnCustomer;
import com.fastloan.jn.mgt.customer.service.JnCustomerService;
import com.fastloan.jn.mgt.process.dao.JnAppApproveUserMapper;
import com.fastloan.jn.mgt.process.model.JnAppApproveUser;
import com.fastloan.jn.mgt.process.model.JnApplicationApprovalReview;
import com.fastloan.jn.mgt.process.model.vo.AppApproveLogVO;
import com.fastloan.jn.mgt.process.model.vo.AppApproveUserVo;
import com.fastloan.jn.mgt.process.service.AppApproveLogService;
import com.fastloan.jn.mgt.process.service.AppApproveUserService;
import com.fastloan.jn.mgt.process.service.ApplicationApprovalReviewService;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 审贷会具体审批信息Service
 *
 * @author chenkai
 *         createTime 2017-02-2017/2/14 11:51
 */
@Service
public class AppApproveUserServiceImpl extends BasicServiceImpl<JnAppApproveUser> implements AppApproveUserService {
    /**
     * 客户信息
     */
    @Autowired
    private JnCustomerService CustomerBasicService;

    /**
     * 进件信息
     */
    @Autowired
    private JnApplicationService tApplicationService;

    @Autowired
    private JnAppApproveUserMapper appApproveUserDao;

    /**
     * 审批历史信息
     */
    @Autowired
    private AppApproveLogService appApproveLogService;

//    @Autowired
//    private MessageService messageService;
//
//    @Autowired
//    private RiskCustomerService riskCustomerService;

    /**
     * 审贷会
     */
    @Autowired
    private ApplicationApprovalReviewService tApplicationApprovalReviewService;

    @Override
    public Map<String, Object> queryApproval(String applicationId, String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("applicationId", applicationId);

        JnApplication application = tApplicationService.selectByPrimaryKey(applicationId);
        List<AppApproveLogVO> appApproveLogList = appApproveLogService.queryApprovalLog(map);
        JnCustomer customer = CustomerBasicService.selectByPrimaryKey(application.getCustomerId());

//        RiskCustomer riskCustomer = new RiskCustomer();
//        riskCustomer.setCustomerId(application.getCustomerId());
//        Integer riskCustomerCount = riskCustomerService.selectCount(riskCustomer);
        map.put("customer", customer);
        map.put("approval", appApproveLogList);
//        map.put("riskCustomer", null != riskCustomerCount ? riskCustomerCount : 0);
        return map;
    }

    @Override
    @DataTable(name = "queryApproval")
    public List<AppApproveUserVo> queryApproval(Map<String, Object> map) {
//        if (ManagerKit.getRoleType().equals(1)) {
//            return appApproveUserDao.queryApprovalReview(map);
//        }
        return appApproveUserDao.queryApproval(map);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int initialization(String userIds, JnApplicationApprovalReview applicationApprovalReview) {
        JnAppApproveUser appApproveUserEntity = new JnAppApproveUser();
        appApproveUserEntity.setApprovalReviewId(applicationApprovalReview.getId());
        List<JnAppApproveUser> approveUserList = appApproveUserDao.select(appApproveUserEntity);
        int initialization = 0;
        if (approveUserList.isEmpty()) {
            applicationApprovalReview.setReviewStatus(1);
            tApplicationApprovalReviewService.updateByPrimaryKeySelective(applicationApprovalReview); //更新审贷会更新信息为已排审
            String[] split = userIds.split(",");
            List<String> list = Arrays.stream(split).collect(Collectors.toList());
            Map<String, Object> map = new HashMap<>();
            JnApplication application = tApplicationService.selectByPrimaryKey(applicationApprovalReview.getAppId());
            map.put("approvalReviewId", applicationApprovalReview.getId());
            map.put("ids", list);
            map.put("applicationId", application.getId());
            initialization = appApproveUserDao.initialization(map);
            for (String userId : list) {
                JnAppApproveUser appApproveUser = new JnAppApproveUser();
                appApproveUser.setApplicationId(application.getId());
                appApproveUser.setUserId(userId);
                appApproveUser.setApprovalReviewId(applicationApprovalReview.getId());
                appApproveUser = appApproveUserDao.selectOne(appApproveUser);
                map.put("appApproveUserId", appApproveUser.getId());
                map.put("userId", userId);
                map.put("approveId", applicationApprovalReview.getApprovalId());
//                switch (ShiroKit.getOrgType()) {
//                    case 0:
//                        messageService.messagePush("审批提示", "您有一个进件需要审批, 进件编号:" +
//                                        application.getApplicationCode(), userId, PER_APPLICATION_APPROVE_APPROVING, map
//                                , Lists.newArrayList(CUSTOMER_MANAGER, EXPERT, PRODUCT_MANAGER));
//                        break;
//                    case 1:
//                        messageService.messagePush("审批提示", "您有一个进件需要审批, 进件编号:" +
//                                        application.getApplicationCode(), userId, APPLICATION_APPROVE_APPROVING, map
//                                , Lists.newArrayList(CUSTOMER_MANAGER, EXPERT, PRODUCT_MANAGER));
//                        break;
//                    default:
//                        break;
//                }

            }
        }
        return initialization;
    }

    @Override
    public JnAppApproveUser queryEndApprovalUser(Integer applicationId, int flag) {
        return appApproveUserDao.queryEndApprovalUser(applicationId, flag);
    }

    @Override
    public int updateStatus(JnAppApproveUser appApproveUser) {
        return appApproveUserDao.updateStatus(appApproveUser);
    }

    @Override
    public Integer queryCount(int userId, int status, boolean flag, int customerType) {
        return appApproveUserDao.queryCount(userId, status, flag, customerType);
    }

    @Override
    public Integer queryApproveAmountCount(int userId, int customerType) {
        return appApproveUserDao.queryApproveAmountCount(userId, customerType);
    }


}
